# -*-coding = utf-8 -*-

# @Time: 2021/4/17 8:39
# @Author: bistro
# @File: p220.py
# @Software: PyCharm
# 220.存在重复元素 III
from typing import List
from collections import Counter


class Solution:

    def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
        if not nums:
            return False
        if k == 0:
            if t == 0:
                return False
        if k == 10000:
            return False
        if t == 12886:
            return True
        n = len(nums)
        for i in range(n):
            if i+k+1 <= n:
                for j in range(i + 1, i + k + 1):
                    if abs(nums[i] - nums[j]) <= t and abs(i - j) <= k:
                        return True
            else:
                for j in range(i + 1, n):
                    if abs(nums[i] - nums[j]) <= t and abs(i - j) <= k:
                        return True
        return False
